
class Item<T> {
    private _value: T;
    private _next: Item<T>;

    constructor(value: T, next: Item<T> = null) {
        this._value = value;
        this._next = next;
    }

    set value(value:T){
        this._value = value;
    }

    get value(){
        return this._value;
    }

    set next(next:Item<T>){
        this._next = next;
    }


    get next(){
        return this._next;
    }

}
interface IStack <T>{
    top(): T;//获取栈顶元素
    push(item: T);//压栈
    pop(): T;//出栈
    empty(): boolean;//是否空栈
    size(): number;//栈大小

}

class Stack<T> implements IStack<T>{
    private _header:Item<T>;
    private _size:number;

    constructor() {
        this._header = new Item<T>(null);
    }

    empty(): boolean {
        return this._size === 0;
    }

    pop(): T {
        let item = this._header.next;
        this._header.next = item.next;
        item.next = null;
        --this._size;
        return item.value;
    }

    push(item: T) {
        let newItem = new Item(item);
        newItem.next = this._header.next;
        this._header.next = newItem;
        ++this._size;
    }

    size(): number {
        return this._size;
    }

    top(): T {
        if(this._size === 0){
            return null;
        }
        return this._header.next.value;
    }

}





